<?php
    include_once './config.php';
    $user = checkLogin(2);
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $action = $_POST['action'];
        $id = $_POST['id'];
        if($action == "complete"){ //完成
            $complete_desc = $_POST['complete_desc'];
            $offline_money = $_POST['offline_money'];
            $online_money = $_POST['online_money'];
            $query = "update whw_order set complete_desc =".quote($complete_desc).",offline_money=".getFloat($offline_money).",online_money=".getFloat($online_money).",status = 1 where id=".getInt($id);
            $res = $objDBCD14->execute($query);
            if($res){
                if($complete_desc){
                    $query = "insert into whw_order_log (user_id,order_id,name,content,createtime) values (".getInt($user['id']).",".getInt($id).",'complete_desc',".quote($complete_desc).",".time().")";
                    $objDBCD14->execute($query);
                }
                if($offline_money){
                    $query = "insert into whw_order_log (user_id,order_id,name,content,createtime) values (".getInt($user['id']).",".getInt($id).",'offline_money',".quote($offline_money).",".time().")";
                    $objDBCD14->execute($query);
                }
                if($online_money){
                    $query = "insert into whw_order_log (user_id,order_id,name,content,createtime) values (".getInt($user['id']).",".getInt($id).",'online_money',".quote($online_money).",".time().")";
                    $objDBCD14->execute($query);
                }
            }
            $msg = "保存成功";
        } else if($action == 'edit'){ //修改
            $name = $_POST['name'];
            $value = $_POST['value'];
            $query = "select * from whw_order where id=".getInt($id);
            $order = $objDBCD14->queryUniqueObject($query);
            $query = "update whw_order set ".$name." = ".quote($value)." where id=".getInt($id);
            $res = $objDBCD14->execute($query);
            if($res){
                $query = "insert into whw_order_log (user_id,order_id,name,content,createtime) values (".getInt($user['id']).",".getInt($id).",".quote($name).",".quote($value).",".time().")";
                $objDBCD14->execute($query);
                $msg = "修改成功";
            } else {
                $error = true;
                $msg = "未作任何修改";
            }
        } else if($action == 'settlement'){ //结算
            $offline_money = $_POST['offline_money'];
            $online_money = $_POST['online_money'];
            $cost = $_POST['cost'];
            $money = $_POST['money'];
            $subsidy = $_POST['subsidy'];
            $pay_money = $_POST['pay_money'];
            $query = "update whw_settlement set offline_money = ".getFloat($offline_money).",online_money =".getFloat($online_money).',cost = '.getFloat($cost).",money = ".getFloat($money).",subsidy = ".getFloat($subsidy).",pay_money = ".getFloat($pay_money).",user_deal_time = ".time().",user_deal = 1 where id = ".getInt($id);
            $objDBCD14->execute($query);
            $msg = "结算成功";
        }
        echo json_encode(['code'=>$error ? 0 : 1,'msg'=>$msg]);
        exit;
    }
    $date = date("Y-m-d",time());
    $user_id = $user['id'];
    if($_GET['date']){
        $date = $_GET['date'];
    } else {
        $date = date("Y-m-d",time());
    }
    $date_start = strtotime($date." 00:00:00");
    $date_end = strtotime($date." 23:59:59");
    // 查询订单列表
    $query = "select * from whw_order where user_id=".getInt($user['id'])." and createtime BETWEEN ".$date_start. " and ".$date_end." order by id desc";
    $rows = $objDBCD14->query($query);
    $orderList = [];
    $keys = ["content","money","imgs","complete_desc","offline_money","online_money","cost","cost_desc","face"];
    $offline_money_total = 0;
    $online_money_total = 0;
    $cost_total = 0;
    $money_total = 0;
    $subsidy_total = 0;
    if ($rows->num_rows) {
        foreach ($rows as $key => $value) {
            $order = json_decode(json_encode($value,JSON_UNESCAPED_UNICODE),true);
            for($i = 0;$i < count($keys);$i++){
               $query = "select user.username from whw_order_log order_log inner join whw_user user on order_log.user_id = user.id where order_log.order_id=".getInt($order['id'])." and order_log.name = ".quote($keys[$i])." order by order_log.id desc";
               $order_log = $objDBCD14->queryUniqueObject($query);
               $order[$keys[$i]."_user"] =  $order_log->username;
            }
            if($order['imgs']){
                $order['imgs'] = explode(",",$order['imgs']);
            }  else {
                $order['imgs'] = [];
            }
            if($order['type'] == 1){
                $offline_money_total += $order['offline_money'];
                $online_money_total += $order['online_money'];
                $cost_total += $order['cost'];
                $money_total += $order['money'];
            } else {
                $subsidy_total += $order['money'];
            }
            $orderList[] =  $order;
        }  
    }
    
    // 是否结算;
    $query = "select a.*,user.username as username from whw_settlement a left join whw_user user on a.admin_id = user.id where a.date = ".quote($date)." and a.user_id=".getInt($user['id']);
    $settlement = $objDBCD14->queryUniqueObject($query);
    $settlement = json_decode(json_encode($settlement),true);
    if(!$settlement['admin_deal']){
        $settlement['offline_money'] = $offline_money_total;
        $settlement['online_money'] = $online_money_total;
        $settlement['cost'] = $cost_total;
        $settlement['money'] = $money_total;
        $settlement['subsidy'] = $subsidy_total;
        $settlement['pay_money'] = $offline_money_total - $money_total - $subsidy_total;
    }
    if($settlement['user_deal_time']){
         $settlement['user_deal_time'] = date("Y-m-d H:i:s",$settlement['user_deal_time'] );
    }
    // 统计今日收入
    $query = "select sum(money) as total from whw_order where type = 1 and user_id =".getInt($user['id'])." and createtime BETWEEN ".$date_start. " and ".$date_end;
    $result = $objDBCD14->queryUniqueObject($query);
    $today_money = $result->total;
    // 未结算天数
    $query = "select count(*) as total from whw_settlement where user_deal = 0 and user_id = ".getInt($user['id']);
    $result = $objDBCD14->queryUniqueObject($query);
    $wait_examine = $result->total;
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>订单管理</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        body{
           background: #f8f8f8; 
           height:100%;
        }
        .container{
            margin: 0;
        }
        .flex{
            display: flex;
        }
        .align-center{
            align-items: center;
        }
        .flex-1{
            flex: 1;
        }
        .header{
            background: #e64340;
            padding: 10px;
            color: #fff;
        }
        .header .user-info{
            display: flex;
            align-items: center;
        }
        .header .user-info .username{
            flex: 1;
            margin: 0 10px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            font-size: 18px;
            color: #fff;
        }
        .avatar{
            width: 60px;
            height: 60px;
            border-radius: 50%;
        }
        .btn-section a{
            display: block;
            background: unset;
            padding: 5px 0;
            color: #fff;
            cursor: pointer;
            font-weight: 500;
        }
        .statistic{
            display: flex;
            align-items: center;
            background: #fff;
            color: #333;
            padding: 5px 0;
            border-radius: 2px; 
            margin-top: 5px;
        }
        .statistic .item{
            flex: 1;
            text-align: center;
           
        }
        .statistic .item .num{
             font-size: 20px;
             font-weight: 900;
        }
        .statistic .item .label{
            color: #333;
            padding-top: 5px;
            display: block;
            font-size: 12px;
        }
        .statistic .item a{
            display: block;
            background: unset;
            padding: 5px 0;
            color: #333;
            cursor: pointer;
            font-weight: 500;
        }
        .worker{
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-top: 10px;
            color: #333;
        }
        .list{
            padding: 10px;
        }
        .list .item{
            background: #fff;
            padding: 15px;
            border-radius: 5px;
            margin: 10px 0;
        }
        .list .item .face-row .face-col{
           display: inline-block; 
           margin: 0 5px 5px 0;
           font-size: 12px;
        }
        .list .item .face-row .face-col .layui-input{
            display: inline-block; 
            width: 40px;
            height: 25px;
            padding-left: 0;
            text-align: center;
        }
        .edit-btn{
            margin-left: 5px;
        }
        .edit-user{
            color: red;
            font-size: 12px;
            /*text-align: center;*/
        }
        .layer-photos img{
            width: 30px;
            height: 30px;
            margin: 0 2px;
        }
         .images{
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            padding: 5px 10px;
        }
        .images .item{
            width: 33.33%;
            min-width: 33.33%;
            max-width: 33.33%;
            position: relative;
            margin-top: 5px;
        }
        .images .item img{
            width: calc(26vw - 10px);
            height: calc(26vw  - 10px);
            border-radius: 5px;
        }
        .images .item i{
            position: absolute;
            top: 5px;
            right: 5px;
        }
        .list-settlement .edit-btn,.item-disabled .edit-btn{
            display: none !important;
        }
        .layui-form-select .layui-input{
            height: 32px;
        }
    </style>
</head>
<body ontouchstart>
    <div class="container" id="container">
        <header class="header">
            <div class="user-info">
                <a href='./setting.php'>
                  <img src="<?=$user['avatar'] ? $user['avatar']: '/images/avatar.png' ?>" class="avatar" />
                </a>
                <a href='./setting.php' class="username">
                    <div style="font-weight: 500"><?=$user['username']?></div>
                    <div style="margin-top: 5px;font-size: 14px"><?=$user['phone']?></div>
                </a>
                <div class="btn-section">
                    <a href='./setting.php'>登录设置</a>
                    <a href='./company.php'>公司制度</a>
                    <a href="javascript:void(0)" class="face">耗材库存</a>
                </div>
            </div>
            <div class="statistic">
                <a class="item" 
                href='javascript:void(0)'>
                    <div class="num"><?=$today_money?></div>
                    <div class="label">今日收入</div>
                </a>
                <a class="item"
                href='./worker-wait-examine.php?date=<?=$date?>'>
                    <div class="num"><?=$wait_examine?></div>
                    <div class="label">未结算天数</div>
                </a>
                <div class="item">
                    <a href="javascript:void(0)" class="order-add" data-type='1'>创建订单</a>
                    <a href="javascript:void(0)" class="order-add" data-type="2">创建补助</a>
                </div>
            </div>
            <form class="layui-form" action="">
                <div class="worker">
                    <div style="width: 130px"></div>
                    <div style="width: 100px">
                        <input type="text" class="layui-input" id="date" value="<?= $date ?>"
                        style="height: 32px">
                    </div>
                </div>
            </form>
        </header>
        <main>
            <?php if(count($orderList)) { ?>
            <div class="list <?= $settlement['admin_deal'] ? 'list-settlement' : '' ?> ">
                <?php if($date != date("Y-m-d",time())){ ?>
                <div class="item">
                    <div style="display: flex;align-items:center;justify-content:space-between">
                        <div>今日小结</div>
                        <div><?=$date?></div>
                        <div>
                            <?php if($settlement['admin_deal'] == 0) { ?>
                                <span>待结算</span>
                            <?php } else { ?>
                                <span>已结算</span>
                            <?php } ?>
                        </div>
                    </div>
                    <div class="flex align-center"
                    style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px;justify-content:space-between">
                        <div class="flex align-center">
                            <div>线上收款：</div>
                            <div class="flex-1 value"><?=$settlement['online_money']?></div>
                        </div>
                        <div class="flex  align-center">
                            <div>线下收款：</div>
                            <div class="flex-1 value"><?=$settlement['offline_money']?></div>
                        </div>
                        <div class="flex align-center">
                            <div>成本：</div>
                            <div class="flex-1 value"><?=$settlement['cost']?></div>
                        </div>
                    </div>
                    <div class="flex align-center"
                    style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px">
                        <div class="flex flex-1 align-center">
                            <div>订单收入：</div>
                            <div class="flex-1 value"><?=$settlement['money']?></div>
                        </div>
                        <div class="flex flex-1 align-center">
                            <div>补助：</div>
                            <div class="flex-1 value"><?=$settlement['subsidy']?></div>
                        </div>
                    </div>
                    <div class="flex align-center"
                    style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px;justify-content:center">
                        <div>
                            今日应上缴公司：<?=$settlement['pay_money']?>元
                        </div>
                        <div style="color: red;margin-left: 5px;font-size: 12px">
                            <?=$settlement['username']?><?=$settlement['admin_deal'] ? '已操作结算' : ''?>
                        </div>
                    </div>
                    <?php if($settlement['user_deal']) { ?>
                     <div style="margin-top: 10px;text-align:center">
                        在<?=$settlement['user_deal_time']?>已确认上缴
                     </div>
                    <?php } else { ?>
                    <form class="layui-form layui-form-pane settlement-form" action="">
                        <div class="flex align-center"
                        style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px;justify-content:center">
                            <input type="hidden" name="offline_money" value="<?=$settlement['offline_money']?>" />
                            <input type="hidden" name="online_money" value="<?=$settlement['online_money']?>" />
                            <input type="hidden" name="cost" value="<?=$settlement['cost']?>" />
                            <input type="hidden" name="money" value="<?=$settlement['money']?>" />
                            <input type="hidden" name="subsidy" value="<?=$settlement['subsidy']?>" />
                            <input type="hidden" name="pay_money_1" value="<?=$settlement['pay_money']?>" />
                            <input type="hidden" name="id" value="<?=$settlement['id']?>" />
                            <button class="layui-btn layui-btn-fluid">确认上缴</button>
                        </div>
                    </form>
                    <?php } ?>
                </div>
                <?php } ?>
                <?php for($i = 0;$i < count($orderList);$i++) { ?>
                    <div class="item <?= in_array($orderList[$i]['status'],[2]) ? 'item-disabled' : '' ?>">
                        <div style="display: flex;align-items:center;justify-content:space-between">
                            <div>第<?=count($orderList) - $i?>单 <?php if($orderList[$i]['type'] == 2) { ?><span>补助</span><?php } ?></div>
                            <div><?=date("Y-m-d H:i:s",$orderList[$i]['createtime'])?></div>
                            <div>
                                <?php if($orderList[$i]['status'] == 0) { ?>
                                    <span>待完成</span>
                                <?php } else if($orderList[$i]['status'] == 1) { ?>
                                    <span>待审核</span>
                                <?php } else if($orderList[$i]['status'] == 2) { ?>
                                    <span>已审核</span>
                                <?php } ?>
                            </div>
                        </div>
                        <!--订单开始-->
                        <?php if($orderList[$i]['type'] == 1) { ?>
                        <div class="flex" style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px">
                            <div>订单内容：<div class="edit-user"><?=$orderList[$i]['content_user']?></div></div>
                            <div class="flex-1 value"><?=$orderList[$i]['content']?></div>
                            <button class="layui-btn layui-btn-xs layui-btn-primary edit-btn" data-name="content" data-id="<?=$orderList[$i]['id']?>">修改</button>
                        </div>
                        <?php if(in_array($orderList[$i]['status'],[0])) { ?>
                        <form class="layui-form layui-form-pane my-form" action="">
                            <div class="flex" style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px">
                                <div>完成情况：</div>
                                <div class="flex-1">
                                   <textarea name="complete_desc" placeholder="请输入说明" required class="layui-textarea" style="min-height: 30px"></textarea>
                                </div>
                            </div>
                            <div class="flex" style="margin-top: 10px">
                                <div class="flex flex-1 align-center">
                                    <div>线下收款：</div>
                                    <div class="flex-1">
                                       <input type="text" name="offline_money" required lay-verify="required" placeholder="输入金额" autocomplete="off" class="layui-input">   
                                    </div>
                                </div>
                                <div class="flex flex-1 align-center"
                                style="margin-left: 10px">
                                    <div>线上收款：</div>
                                    <div class="flex-1">
                                       <input type="text" name="online_money" required lay-verify="required" placeholder="输入金额" autocomplete="off" class="layui-input">   
                                    </div>
                                </div>
                            </div>
                            <div style="margin-top: 10px">
                                 <input type="hidden" name="id" value="<?=$orderList[$i]['id']?>">   
                                <button class="layui-btn layui-btn-fluid">提交</button>
                            </div>
                        </form>
                        <?php } else if(in_array($orderList[$i]['status'],[1,2])) { ?>
                        
                        <div class="flex" style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px">
                            <div>完成情况：<div class="edit-user"><?=$orderList[$i]['complete_desc_user']?></div></div>
                            <div class="flex-1 value"><?=$orderList[$i]['complete_desc']?></div>
                            <button class="layui-btn layui-btn-xs  layui-btn-primary edit-btn"
                            data-name="complete_desc" data-id="<?=$orderList[$i]['id']?>">修改</button>
                        </div>
                        <div class="flex" style="margin-top: 10px">
                            <div class="flex flex-1">
                                <div>线下收款：<div class="edit-user"><?=$orderList[$i]['offline_money_user']?></div></div>
                                <div class="flex-1 value"><?=$orderList[$i]['offline_money']?></div>
                                <button class="layui-btn layui-btn-xs layui-btn-primary edit-btn"
                                data-name="offline_money" data-id="<?=$orderList[$i]['id']?>">修改</button>
                            </div>
                            <div class="flex flex-1"
                            style="margin-left: 10px">
                                <div>线上收款：<div class="edit-user"><?=$orderList[$i]['online_money_user']?></div></div>
                                <div class="flex-1 value"><?=$orderList[$i]['online_money']?></div>
                                <button class="layui-btn layui-btn-xs  layui-btn-primary edit-btn"
                                data-name="online_money" data-id="<?=$orderList[$i]['id']?>">修改</button>
                            </div>
                        </div>
                        <?php } if(in_array($orderList[$i]['status'],[2])) { ?>
                        <div class="flex" style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px">
                            <div class="flex flex-1">
                                <div>成本：<div class="edit-user"><?=$orderList[$i]['cost_user']?></div></div>
                                <div class="value"><?=$orderList[$i]['cost']?></div>
                                <button class="layui-btn layui-btn-xs layui-btn-primary edit-btn"
                                data-name="cost" data-id="<?=$orderList[$i]['id']?>">修改</button>
                            </div>
                            <div class="flex flex-1 align-center"
                            style="margin-left: 10px">
                            </div>
                        </div>
                        <div class="flex" style="margin-top: 10px;">
                            <div>成本说明：<div class="edit-user"><?=$orderList[$i]['cost_desc_user']?></div></div>
                            <div class="flex-1 value"><?=$orderList[$i]['cost_desc']?></div>
                            <button class="layui-btn layui-btn-xs  layui-btn-primary edit-btn" data-name="cost_desc" data-id="<?=$orderList[$i]['id']?>">修改</button>
                        </div>
                        <div class="flex" style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px">
                            <div class="flex flex-1">
                                <div>消耗库存：<div class="edit-user"><?=$orderList[$i]['face_user']?></div></div>
                                <div class="flex-1 face-row value">
                                    <?php
                                     $face = json_decode($orderList[$i]['face'],true);
                                    ?>
                                    <?php for($j = 0;$j < count($face);$j++) { ?>
                                        <div class="face-col">
                                           <span><?=$face[$j]['name']?></span>
                                           <span><?=$face[$j]['number']?>个</span>
                                        </div>
                                    <?php } ?>
                                </div>
                                <button class="layui-btn layui-btn-xs layui-btn-primary edit-btn"
                                data-name="face" data-id="<?=$orderList[$i]['id']?>">修改</button>
                            </div>
                        </div>
                        <div class="flex" style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px">
                            <div>此单收入：<div class="edit-user"><?=$orderList[$i]['money_user']?></div></div>
                            <div class="value"><?=$orderList[$i]['money']?></div>
                            <button class="layui-btn layui-btn-xs  layui-btn-primary edit-btn" 
                             data-name="money" data-id="<?=$orderList[$i]['id']?>">修改</button>
                        </div>
                        <?php } ?>
                        <!--订单结束-->
                        <?php } else {?>
                        <!--补助开始-->
                        <div class="flex" style="margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 10px">
                            <div class="flex flex-1">
                                <div>金额：<div class="edit-user"><?=$orderList[$i]['money_user']?></div></div>
                                <div class="value"><?=$orderList[$i]['money']?></div>
                                <button class="layui-btn layui-btn-xs layui-btn-primary edit-btn"
                                data-name="money" data-id="<?=$orderList[$i]['id']?>">修改</button>
                            </div>
                            <div class="flex flex-1 align-center"
                            style="margin-left: 10px">
                            </div>
                        </div>
                        <div class="flex" style="margin-top: 10px;">
                            <div>说明：<div class="edit-user"><?=$orderList[$i]['content_user']?></div></div>
                            <div class="flex-1 value"><?=$orderList[$i]['content']?></div>
                            <button class="layui-btn layui-btn-xs  layui-btn-primary edit-btn" data-name="content" data-id="<?=$orderList[$i]['id']?>">修改</button>
                        </div>
                        <div class="flex" style="margin-top: 10px;">
                            <div>图片：<div class="edit-user"><?=$orderList[$i]['imgs_user']?></div></div>
                            <div class="flex-1 layer-photos value">
                                <?php for($img_i = 0;$img_i < count($orderList[$i]['imgs']);$img_i++){ ?>
                                <img  layer-src="<?=$orderList[$i]['imgs'][$img_i]?>" src="<?=$orderList[$i]['imgs'][$img_i]?>" />
                                <?php } ?>
                            </div>
                            <button class="layui-btn layui-btn-xs  layui-btn-primary edit-btn" data-name="imgs" data-id="<?=$orderList[$i]['id']?>">修改</button>
                        </div>
                        <?php } ?>
                        <!--补助结束-->
                    </div>
                <?php } ?>
            </div>
            <?php } else { ?>
            <img src="./images/data-empty.png" style="width: 200px;height:auto;display:block;margin: 10vh auto" />
            <?php } ?>
        </main>
    </div>
    <!-- 引入 layui.js -->
    <script src="./layui/layui.js"></script>
    <script>
        layui.use(['layer', 'form','laydate','upload'], function(){
            var  $ = layui.$,  layer = layui.layer,form = layui.form,laydate = layui.laydate,upload = layui.upload;
            var user = '<?php echo json_encode($user) ?>';
            user = JSON.parse(user)
            // 日期
            laydate.render({
                elem: '#date',
                done: function(value, date, endDate){
                   var user_id = $("#worker").val()
                   location.href = 'worker.php?date='+value
                }
            });
            // 图片
            layer.photos({
              photos: '.layer-photos'
              ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });
            // 删除
            $(document).on('click', '.del-img', function() {
                $(this).parent().remove()
            })
            // 更新表单渲染
            form.render('select');
            // 耗材管理
            $(".face").click(function(){
                layer.open({
                  type: 2, 
                  title: "我的耗材库存",
                  area: ['90vw','80vh'],
                  content: './face.php?user_id='+user.id,
                  shadeClose: false,
                  fixed: true,
                  offset: 'auto', // 自动偏移
                  success: function(layero, index){
                    
                  },
                });
            })
            // 创建订单
            $(".order-add").click(function(){
                var type = $(this).data("type")
                layer.open({
                  type: 2, 
                  title: type == 1 ? "创建订单" : '创建补助',
                  area: ['90vw',user.can_order == 1 ? (type == 1 ? '300px' : '90vh'): '200px'],
                  content: './order-add.php?user_id='+user.id + "&type="+type,
                  shadeClose: false,
                  fixed: true,
                  offset: 'auto', // 自动偏移
                  success: function(layero, index){
                    
                  },
                });
            })
            // 提交完成
            $(".my-form").submit(function(){
                var formValues = $(this).serializeArray();
                var param = {}
                $.each(formValues, function(i, field){
                    if(field.name == "face_number[]" || field.name == "face_name[]"){
                         if(!param.hasOwnProperty(field.name)){
                            param[field.name]=[];
                        }
                        param[field.name].push(field.value)
                    } else {
                        param[field.name] = field.value
                    }
                });
                param.action = "complete"
                $.post("worker.php",param,function(res){
                    res = JSON.parse(res)
                    layer.msg(res.msg);
                    if(res.code == 1){
                        setTimeout(function(){
                           location.reload()
                        },1000)
                    }
                })
                return false
            })
            // 结算
            $(".settlement-form").submit(function(){
                var formValues = $(this).serializeArray();
                var param = {}
                $.each(formValues, function(i, field){
                   param[field.name] = field.value
                });
                param.action = "settlement"
                $.post("worker.php",param,function(res){
                    res = JSON.parse(res)
                    layer.msg(res.msg);
                    if(res.code == 1){
                        setTimeout(function(){
                          location.reload()
                        },1000)
                    }
                })
                return false
            })
            
            // 修改
            $(document).on('click', '.edit-btn', function() {
                var obj = $(this)
                var name = obj.data("name")
                var id = obj.data("id")
                var value = obj.siblings(".value").text()
                var title = ""
                var formType = 0
                switch(name){
                    case 'content':
                        title = "订单内容"
                        formType = 2
                    break;
                    case 'complete_desc':
                        title = "完成情况"
                        formType = 2
                    break;
                    case 'cost_desc':
                        title = "成本说明"
                        formType = 2
                    break;
                    case 'offline_money':
                        title = "线下收款"
                        formType = 0
                    break;
                    case 'online_money':
                        title = "线上收款"
                        formType = 0
                    break;
                    case 'imgs':
                        title = "修改图片"
                        formType = 20
                    break;
                }
                if(formType == 20){
                    var imgsHtml = '<div class="images">'
                    obj.siblings(".value").find("img").each(function(){
                        var imgUrl = $(this).attr("src")
                        imgsHtml += '<div class="item"><img class="layui-img" src="'+imgUrl+'"/><i class="layui-icon layui-icon-close-fill del-img"></i></div>'
                    })
                    imgsHtml += '</div>'
                    imgsHtml += ' <div class="layui-input-block" style="margin-top: 10px;margin-left: 10px;"><button type="button" class="layui-btn" id="upload"><i class="layui-icon">&#xe67c;</i>上传图片</button></div>'
                    layer.open({
                      type: 1, 
                      title,
                      area: ['80vw','60vh'],
                      content: imgsHtml,
                      btn: ['保存', '取消'],
                      success: function(){
                        var uploadInst = upload.render({
                            elem: '#upload' //绑定元素
                            ,url: 'upload.php' //上传接口
                            ,accept: "images"
                            ,multiple: true
                            ,done: function(res){
                              if(res.code == 1){
                                $(".images").append('<div class="item"><img class="layui-img" src="'+res.data.src+'"/><i class="layui-icon layui-icon-close-fill del-img"></i></div>')   
                              }
                              //上传完毕回调
                            }
                            ,error: function(){
                              //请求异常回调
                            }
                        });  
                      },
                      yes: function(lay_index,layero){
                        // 获取图片
                        var imgs = []
                        var html = ''
                        layero.find('.layui-img').each(function(){
                            var imgUrl = $(this).attr("src")
                            imgs.push(imgUrl)
                            html += '<img layer-src="'+imgUrl+'" src="'+imgUrl+'" />'
                        })
                        var value = imgs.join(",") 
                        $.post("worker.php",{id,action:'edit',name: "imgs",value},function(res){
                            res = JSON.parse(res)
                            layer.msg(res.msg);
                            if(res.code == 1){
                                obj.siblings(".value").html(html)
                                layer.close(lay_index);
                                location.reload()
                            }
                        })
                      }
                    });
                }else {
                    layer.prompt({
                      formType,
                      value,
                      title,
                      area: formType == 1 ? ['80vw', '60px']  : ['80vw', '100px']
                    }, function(value, index, elem){
                        $.post("worker.php",{id,action:'edit',name,value},function(res){
                            res = JSON.parse(res)
                            layer.msg(res.msg);
                            if(res.code == 1){
                                obj.siblings(".value").text(value)
                                layer.close(index);
                                location.reload()
                            }
                        })
                    });
                }
               
            })
        });
    </script>  
</body>
</html>